#include "ringQueue.hpp"
#include <cstdlib>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

void *consumer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (true)
    {
        int x;
        //1.从环形结构中获取数据或者任务对象 
        rq->pop(&x);
        //2.推送到环形队列中
        sleep(1);
        
        cout<<"消费："<<x<<" [" << pthread_self() <<"]"<<endl;
    }
}
void *productor(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (true)
    {
        //1.构建数据或者任务对象 
        int x =rand()%100 +1;
        //2.推送到环形队列中
        rq->push(x);//完成生产的过程
        cout<<"生产："<<x<<" [" << pthread_self() <<"]"<<endl;

        //sleep(1);
        
    }
    
}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid());
    RingQueue<int> *rq = new RingQueue<int>();
    //rq->debug();
    pthread_t c[3],p[2];
    pthread_create(c,nullptr,consumer,(void*)rq);
    pthread_create(c+1,nullptr,consumer,(void*)rq);
    pthread_create(c+2,nullptr,consumer,(void*)rq);
    pthread_create(p,nullptr,productor,(void*)rq);
    pthread_create(p+1,nullptr,productor,(void*)rq);

    for(int i = 0;i<3;i++)pthread_join(c[i],nullptr);
    for(int i = 0;i<2;i++)pthread_join(p[i],nullptr);

    return 0;
}